let ids = [];

let idsMaxLength = 20;
function addIds(id) {
  if (!ids.includes(id)) {
    ids.push(id)
  } else {
    return false
  }
}
function Collect() {
  let contentList = document.querySelectorAll('.feeds-container section');
  contentList.forEach(dom => {
    let a = dom.querySelector('div a');
    if (a) {
      // console.log(a,'a.href')
      let arr = a.href.split('/');
      let targetId = arr[arr.length - 1];
      // console.log(targetId,'targetId')
      addIds(targetId);
      // 调用函数发送数据
      // const dataToSend = { key: ids , type:'Start'}; // 你要发送的数据对象
      // sendDataToServer(dataToSend);
    } else {
      //   console.log('没有找到')
    };
  })
}
// let time = setTimeout()
let nextSrollTop = window.document.scrollingElement.scrollTop;
let currentSrollTop = window.document.scrollingElement.scrollTop;
function fn() {
  // setout
  window.setTimeout(() => {
    let endContainer = document.querySelector('.end-container');
    if (!endContainer) {
      window.document.scrollingElement.scrollTop += 100;
      Collect();
      fn()
    } else {
      window.document.scrollingElement.scrollTop += 2000;
      Collect()
      const dataToSend = { key: '重庆旅游', data: ids }; // 你要发送的数据对象
      sendDataToServer(dataToSend);
    }

  }, 300)
}
fn()

const PORT = 8090;
const url = `http://localhost:${PORT}/data`;

function sendDataToServer(data) {
  // 设置fetch选项
  const requestOptions = {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify(data)
  };

  // 发起POST请求
  fetch(url, requestOptions)
    .then(response => {
      // 检查响应状态
      if (!response.ok) {
        throw new Error('Network response was not ok');
      }
      // 解析JSON格式的响应数据
      return response.json();
    })
    .then(responseData => {
      // 处理接收到的响应数据
      console.log('Received response from server:', responseData);
    })
    .catch(error => {
      // 处理错误
      console.error('There was a problem with the fetch operation:', error);
    });
}


